package org.fle4y.example.aio.chat.ui;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.nio.channels.CompletionHandler;
import java.nio.charset.Charset;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import org.fle4y.common.utils.log.LogUtil;
import org.fle4y.example.aio.chat.ChatMessage;
import org.fle4y.example.aio.chat.ChatType;


/**
 * author : xiehui<br>
 * company : northking <br>
 * Create at: 2012-6-13<br>
 * @version : 1.0<br>
 * mailto:flexie@foxmail.com<br>
 *  Description : 客户端
 */
@SuppressWarnings("unchecked")
public class ClientFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	private static final Logger log = LogUtil.getLogger(ClientFrame.class.getName());
	public LoginPanel loginPanel;
	public ClientPanel clientPanel;
	public CardLayout layout;
	public int width=600;
	public int height=400;
	public boolean running=true;
	public String userNo;
	public AsynchronousSocketChannel channel;
	public ByteBuffer buffer ;
	public int count;
	public Future future ;
	public ClientFrame(){
		initComponent();
	}
	private void initComponent() {
		buffer=ByteBuffer.allocate(1024);
		loginPanel=new LoginPanel(this);
		clientPanel=new ClientPanel(this);
		layout=new CardLayout();
		layout.addLayoutComponent(loginPanel,"loginPanel");
		layout.addLayoutComponent(clientPanel,"clientPanel");
		this.add(loginPanel);
		this.add(clientPanel);
		this.setLayout(layout);
		Toolkit tool=Toolkit.getDefaultToolkit();
		Dimension ds = tool.getScreenSize();
		int screenHeight=(int)ds.getHeight();
		int screenWidth=(int)ds.getWidth();
		this.setTitle("AIO演示客户端");
		this.setSize(width, height);
		this.setLocation((screenWidth-width)/2,(screenHeight-height)/2);
		this.setResizable(false);
		String plaf="javax.swing.plaf.nimbus.NimbusLookAndFeel";
		try {
			UIManager.setLookAndFeel(plaf);
			SwingUtilities.updateComponentTreeUI(this);
		} catch (Exception e) {
		}
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		new Thread(new ClientRobot(this)).start();
	}

	private class ClientRobot implements Runnable{
		public ClientFrame client;
		public boolean running=true;
		public ClientRobot(ClientFrame client) {
			super();
			this.client = client;
		}
		public void run() {
				try {
					channel = AsynchronousSocketChannel.open();
					future= channel
							.connect(new InetSocketAddress("localhost", 8888));
					future.get();
					read();
				}  catch (Exception e) {
					e.printStackTrace();
				}
		}
		private void read(){
				buffer.clear();
				channel.read(buffer,client,new CompletionHandler<Integer,ClientFrame>(){
				public void completed(Integer result, ClientFrame attachment) {
					if(result>1){
						log.info("读到数据");
						log.info(channel.toString());
						ChatMessage msg=new ChatMessage();
						buffer.flip();
						byte[] bs=new byte[buffer.limit()];
						buffer.get(bs);
						String content = new String(bs,Charset.forName("UTF-8"));
						log.info("读取到消息: "+content);
						String[] strs =content.split(":");
						msg.setType(strs[0]);
						msg.setName(strs[1]);
						msg.setMessage(strs[2]);
						msg.setContent(ByteBuffer.wrap(bs));
						if(msg.getType().equals(ChatType.FRESH)){
							String name=msg.getMessage();
							String[] names=name.split("@");
							log.info("name size  --->"+names.length);
							DefaultListModel model=clientPanel.model;
							model.clear();
							for(String s:names){
										model.addElement(s);
							}
						}
						else if(msg.getType().equals(ChatType.CHAT)){
							count++;
							if(count%10==0){
								clientPanel.jta01.setText("");
							}
							String oldMsg= clientPanel.jta01.getText();
							clientPanel.jta01.setText(oldMsg+msg.getName()+"说 :"+msg.getMessage()+"\n");
						}
						read();
					}else if(result<1){
						return ;
					}
				}
				public void failed(Throwable exc, ClientFrame attachment) {
	
				}});
		}
	}
}
